﻿#include "stdafx.h"
#include "SheetListCtrl.h"
#include "ui_SheetListCtrl.h"
#include "LoginUserInfo.h"
#include "FrameInclude.h"

#include <QMessageBox>
#include <QDateTime>
#include <QInputDialog>

SheetListCtrl::SheetListCtrl(QWidget *parent)
: QWidget(parent), ui(new Ui::SheetListCtrl)
{
	ui->setupUi(this);
	isShowCheck = false;
	initData();
	initUI();
	initConnect();
	initQueryCondition();
}

SheetListCtrl::~SheetListCtrl()
{
	delete pTableModel;
	delete ui;
}

/***********************************************************************************************************************************/
bool SheetListCtrl::initialise(QString sql, int listid, int sheetBox, bool isCheck)
{
	LoginUserInfo * pUserInfo = LoginUserInfo::getInstance();
	QString strCurUserId = pUserInfo->getUserId();
	iSheetBox = sheetBox;

	CHK_EXP_RUN(sql == "",sql = "1=1");
	sql.replace("[CURUSERID]", strCurUserId);
	strCurFilterSql = sql;
	isShowCheck = isCheck;
	iCurPage = 1;//每次点击操作票树均显示第一页
	initHeaderInfo();
	initColumnInfo(listid);
	initSheetList(sql);
	return true;
}

void SheetListCtrl::setTopWidget(bool isVisible)
{
	ui->topWidget->setVisible(isVisible);
}

/***********************************************************************************************************************************/
void SheetListCtrl::on_btnFirst_clicked()
{
	iCurPage = 1;
	ui->strPageNum->setText(QString("%1").arg(iCurPage));
	//initSheetList(strCurFilterSql);
	on_btnQuery_clicked();
}

void SheetListCtrl::on_btnUp_clicked()
{
	if (iCurPage<2)
	{
		return;
	}
	iCurPage--;
	ui->strPageNum->setText(QString("%1").arg(iCurPage));
	on_btnQuery_clicked();
}

void SheetListCtrl::on_btnDown_clicked()
{
	if (iCurPage > iTotalPage - 1)
	{
		return;
	}
	iCurPage++;
	ui->strPageNum->setText(QString("%1").arg(iCurPage));
	on_btnQuery_clicked();
}

void SheetListCtrl::on_btnLast_clicked()
{
	iCurPage = iTotalPage;
	ui->strPageNum->setText(QString("%1").arg(iCurPage));
	on_btnQuery_clicked();
}

void SheetListCtrl::on_btnJump_clicked()
{
	int pagenum = ui->strPageNum->text().toInt();
	if (pagenum < 1 || pagenum > iTotalPage || pagenum == iCurPage)
	{
		ui->strPageNum->setText(QString::number(iCurPage));
		return;
	}
	iCurPage = pagenum;
	on_btnQuery_clicked();
}

void SheetListCtrl::on_btnQuery_clicked()
{
	QString strfiltersql = "";
	QString sheetname = ui->strSheetName->text();
	if (sheetname == "")
	{
		strfiltersql = strCurFilterSql;
	}
	else
	{
		strfiltersql = strCurFilterSql + QString(" AND (SHEETNAME LIKE '%%1%')").arg(sheetname);
	}

	if (ui->comAudit->isVisible())
	{
		QString strpyjg = ui->comAudit->currentText();
		if (strpyjg == tr("qualified"))
		{
			strfiltersql += " AND PYJG = 1";
		}
		if (strpyjg == tr("unqualified"))
		{
			strfiltersql += " AND PYJG = 0";
		}
	}
	initSheetList(strfiltersql);
}


void SheetListCtrl::on_sheetListView_doubleClicked(const QModelIndex & index)
{
	if (index.isValid())
	{
		int row = index.row();

		int sheetID = pTableModel->records().getCollectInt(row, "SHEETID");
		QString sheetName = pTableModel->records().getCollectStr(row, "SHEETNAME");
		QString plantime = pTableModel->records().getCollectStr(row, "PLANSTARTTIME");
		if (sheetID > 0)
			emit doubleClicked(sheetID, iSheetBox, sheetName);

		//sheetID<=0弹出提醒
	}
}
void SheetListCtrl::on_sheetListView_clicked(const QModelIndex & index)
{
	if (index.isValid())
	{
		int row = index.row();

		int sheetID = pTableModel->records().getCollectInt(row, "SHEETID");
		QString sheetName = pTableModel->records().getCollectStr(row, "SHEETNAME");
		
		if (sheetID > 0)
			emit clicked(sheetID, sheetName);

		
	}
}

/***********************************************************************************************************************************/
void SheetListCtrl::initUI()
{
	pTableModel = new QListCtrlTableModel();
	ui->sheetListView->setModel(pTableModel);
}

void SheetListCtrl::initData()
{
	iCurPage = 1;
	iTotalPage = 0;
}

void SheetListCtrl::initConnect()
{

}

void SheetListCtrl::initHeaderInfo()
{
	ui->btnDelete->setVisible(false);
	ui->btnUpdateName->setVisible(false);
	ui->btnAudit->setVisible(false);
	ui->labAudit->setVisible(false);
	ui->comAudit->setVisible(false);
	ui->btnGLJXDOMS->setVisible(false);

	int iindex = strCurFilterSql.indexOf("SHEETSTATUSID =");
	if (strCurFilterSql.mid(iindex + 16, 2) == "1 ")
	{
		ui->btnDelete->setVisible(true);
		ui->btnUpdateName->setVisible(true);
	}
	if (strCurFilterSql.indexOf("PYJG<0") != -1)
	{
		ui->btnAudit->setVisible(true);
	}
	if (strCurFilterSql.indexOf("PYJG>=0") != -1)
	{
		ui->labAudit->setVisible(true);
		ui->comAudit->setVisible(true);
	}
}

void SheetListCtrl::initColumnInfo(int listid)
{
	QRecord dsRecord;
	QString sql = QString("select fieldname,columnname,columnwidth,isvisual from WS_SHEETLISTMSG where moduletype=2 and listid=%1  order by columnid")
		.arg(listid);
	dsRecord.select(sql);
	listColoumnInfo.clear();
	for (int i = 0; i < dsRecord.rowCount();i++)
	{
		SHEETLIST_COLUMNINFO infoOne;
		infoOne.strFieldName = dsRecord.getCollectStr(i,"FIELDNAME");
		infoOne.strColoumnName = dsRecord.getCollectStr(i, "COLUMNNAME");
		infoOne.iColoumnWidth = dsRecord.getCollectInt(i, "COLUMNWIDTH");
		infoOne.iIsVisual = dsRecord.getCollectInt(i, "ISVISUAL");
		listColoumnInfo.append(infoOne);
	}
}

void SheetListCtrl::initSheetList(QString filterSql)
{
	QString countSql = "select count(1) as num from ws_sheetindex where " + filterSql;
	QRecord dsRecord;
	dsRecord.select(countSql);

	int totalrow = 0;
	iTotalPage = 0;
	totalrow = dsRecord.getCollectInt(0, "NUM");

	QVariant variant(totalrow);
	QString strtotalrow = tr("total") + variant.toString() + tr("tiao");
	ui->labTotalNum->setText(strtotalrow);

	/*
	* 获取QTableView的高度和水平标题高度，计算显示行数，避免竖直滚动条出现
	*/

	int iTabViewHeight = ui->sheetListView->height();
	int iHeadHeight = ui->sheetListView->horizontalHeader()->height();
	if (iHeadHeight == 0){
		iHeadHeight = 30;
	}
	int iRowNumber = (iTabViewHeight - iHeadHeight)/30;
	CHK_EXP_RUN( iRowNumber == 0, iRowNumber = 14 );
	if (0 == totalrow%iRowNumber)
	{
		iTotalPage = totalrow / iRowNumber;
	}
	else
	{
		iTotalPage = totalrow / iRowNumber + 1;
	}

	QVariant variantpage(iTotalPage);
	ui->strPageNum->setText(QString("%1").arg(iCurPage));
	QString strtotalpage = tr("total") + variantpage.toString() + tr("page");
	ui->labTotalPage->setText(strtotalpage);

	QString strcolumname = "";
	for (int i = 0; i < listColoumnInfo.count();i++)
	{
		strcolumname += listColoumnInfo[i].strFieldName+",";
	}
	strcolumname = strcolumname.left(strcolumname.count() - 1);

#ifdef DB_DM6
    QString sql = "select " + strcolumname + " from ws_sheetindex where " + filterSql + QString(" limit %1,").arg(iRowNumber) + QString::number((iCurPage - 1)*iRowNumber) + " order by maketime desc"; //WIN DM6达梦数据库

#elif defined DB_DM7
    QString sql = "select " + strcolumname + " from ws_sheetindex where " + filterSql + QString(" limit %1, %2 ").arg((iCurPage - 1)*iRowNumber).arg(iRowNumber) + " order by maketime desc"; //Linux达梦数据库

#else
	QString sql = "select " + strcolumname + " from (select " + strcolumname + ",rownum as con from(select " + strcolumname + " from ws_sheetindex where " + filterSql + " order by maketime desc) where rownum< " + QString::number((iCurPage)*iRowNumber)+") where con >"+ QString::number((iCurPage - 1)*iRowNumber);	//sqlserver数据库
#endif 
	
	pTableModel->select(sql, isShowCheck);
	pTableModel->setCol(0, CF_PRIMARY);
	int columshowcount = 0;
	for (int i = 1; i < listColoumnInfo.count(); i++)
	{
		if (listColoumnInfo[i].iIsVisual = 1)
		{
			pTableModel->setCol(i, CF_SHOW, listColoumnInfo[i].strColoumnName);
		}
	}

	pTableModel->refresh();

	for (int i = 1; i < listColoumnInfo.count(); i++)
	{
		if (listColoumnInfo[i].iIsVisual = 1)
		{
			ui->sheetListView->setColumnWidth(columshowcount, listColoumnInfo[i].iColoumnWidth);
			columshowcount++;
		}
	}

	pTableModel->refresh();
	updatePageTurningBtn();
}

void SheetListCtrl::updatePageTurningBtn()
{
	if (iTotalPage <= 1){
		ui->btnFirst->setEnabled(false);
		ui->btnUp->setEnabled(false);
		ui->btnDown->setEnabled(false);
		ui->btnLast->setEnabled(false);
	}else{
		if (iCurPage == 1)
		{
			ui->btnFirst->setEnabled(false);
			ui->btnUp->setEnabled(false);
			ui->btnDown->setEnabled(true);
			ui->btnLast->setEnabled(true);
		}else if (iCurPage == iTotalPage){
			ui->btnFirst->setEnabled(true);
			ui->btnUp->setEnabled(true);
			ui->btnDown->setEnabled(false);
			ui->btnLast->setEnabled(false);
		}else{
			ui->btnFirst->setEnabled(true);
			ui->btnUp->setEnabled(true);
			ui->btnDown->setEnabled(true);
			ui->btnLast->setEnabled(true);
		}
	}
}

void SheetListCtrl::on_strPageNum_editingFinished()
{
	on_btnJump_clicked();
}

int SheetListCtrl::getNewID(QString tablename, QString colname)
{
	QString sqlSelect = QString("SELECT MAX(%1) MAXID FROM %2").arg(colname).arg(tablename);
	QRecord dsRecord;
	dsRecord.select(sqlSelect);

	int iMaxID = -1;
	iMaxID = dsRecord.getCollectInt(0, "MAXID");
	if (iMaxID < 0)
	{
		return -1;
	}

	return iMaxID+1;
}

void SheetListCtrl::on_btnDelete_clicked()
{
	
}

QList<int> SheetListCtrl::getCheckedSheetID()
{
	QList<int> iSheetIdList;
	int iRowCount = pTableModel->records().rowCount();
	for (int i = 0; i < iRowCount; i++)
	{
		if (pTableModel->getChecked(i))
		{
			int iSHEETID = pTableModel->records().getCollectInt(i, "SHEETID");
			iSheetIdList.append(iSHEETID);
		}
	}
	return iSheetIdList;
}

void SheetListCtrl::on_btnUpdateName_clicked()
{

}

bool SheetListCtrl::modifySheetName(QString newSheetName, int sheetId)
{
	return true;
}

void SheetListCtrl::on_btnAudit_clicked()
{
	
}

void SheetListCtrl::discussSuccess(bool bSuccess)
{
	if (bSuccess)
	{
		qMsgBoxInfo(tr("Discuss success!"));
		initSheetList(strCurFilterSql);
	}
	else
	{
		qMsgBoxAlert(tr("Discuss failed!"));
	}
}

void SheetListCtrl::initQueryCondition()
{
	ui->strSheetName->setText("");
}

/***********************************************************************************************************************************/

QListCtrlTableModel::QListCtrlTableModel(QObject * parent)
	:QRecordTableModel(parent)
{
	isShowCheck = false;
}

QListCtrlTableModel::~QListCtrlTableModel()
{

}

bool QListCtrlTableModel::select(QString sql,bool isCheck)
{
	bool ret = QRecordTableModel::select(sql);
	listCheck.clear();
	for (int i = 0; i < dsRecords.rowCount();i++)
	{
		listCheck.append(false);
	}
	isShowCheck = isCheck;
	return ret;
}

Qt::ItemFlags QListCtrlTableModel::flags(const QModelIndex &index) const
{
	if (!index.isValid())
		return 0;
	int row = index.row();
	if (0 == index.column())
	{
		return isShowCheck ? Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable : QRecordTableModel::flags(index);
	}

	return QRecordTableModel::flags(index);
}

QVariant QListCtrlTableModel::data(const QModelIndex &index, int role) const
{
	int row = index.row();
	int column = index.column();
	if (role == Qt::CheckStateRole && 0 == column && isShowCheck)
	{
		if (listCheck[row])
		{
			return Qt::Checked;
		}
		return Qt::Unchecked;
	}

	return QRecordTableModel::data(index, role);
}

bool QListCtrlTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	int row = index.row();
	if (role == Qt::CheckStateRole && 0 == index.column() && isShowCheck)
	{
		if (value == Qt::Unchecked)
		{
			listCheck[row] = false;
		}
		else
		{
			listCheck[row] = true;
		}
		return true;
	}

	return QRecordTableModel::setData(index, value, role);
}

bool QListCtrlTableModel::getChecked(int i)
{
	if (i >= 0 && i < listCheck.count())
	{
		return listCheck.at(i);
	}
	else
	{
		return false;
	}
}
